package com.jyw.custom.huaxi.oa.controller;

import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.domain.model.WechatLoginBody;
import com.jyw.common.exception.ServiceException;
import com.jyw.custom.huaxi.oa.domain.dto.HuaxiDeptPushDto;
import com.jyw.custom.huaxi.oa.domain.dto.HuaxiUserPushDto;
import com.jyw.custom.huaxi.oa.service.IHuaxiOaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

@RestController
@RequestMapping("/custom/huaxi/oa/user")
@Api(tags = "用户管理")
@Slf4j
public class UserController {

    @Resource
    private IHuaxiOaService oaService;

    @PostMapping("push")
    @ApiOperation("推送用户数据")
    public CommonResult push(String sendMsg){
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(HuaxiUserPushDto.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            HuaxiUserPushDto unmarshal = (HuaxiUserPushDto)unmarshaller.unmarshal(new StringReader(sendMsg));
            oaService.synchronizeUser(unmarshal);
            return CommonResult.success("同步"+unmarshal.getBody().getCtCareProvList().size()+"条用户数据");
        } catch (JAXBException e) {
            log.error("解析华西OA系统同步数据Xml错误",e);
            throw new ServiceException("解析数据异常");
        }
    }




}
